package cn.tedu.four.service.impl;

import cn.tedu.four.ex.ServiceException;
import cn.tedu.four.mapper.AlbumMapper;
import cn.tedu.four.pojo.dto.AlbumAddNewDTO;
import cn.tedu.four.pojo.entity.Album;
import cn.tedu.four.pojo.vo.AlbumListItemVO;
import cn.tedu.four.pojo.vo.AlbumVO;
import cn.tedu.four.service.AlbumService;
import cn.tedu.four.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: AlbumService
 * Package: cn.tedu.four.service.impl
 * Description:
 *
 * @Author: @Albert
 * @Version: 1.0
 */
@Slf4j
@Service
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    AlbumMapper mapper;

    public AlbumServiceImpl() {
       log.info("创建业务对象AlbumServiceImpl");
    }

    @Override
    public void addNew(AlbumAddNewDTO album) {
        // TODO 保证此相册的名称是唯一的
        int i = mapper.countByName(album.getName());
        if(i>0){
            String message="添加相册失败，尝试添加称被使用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Album a = new Album();
        BeanUtils.copyProperties(album,a);
        mapper.insert(a);
    }

    @Override
    public List<AlbumListItemVO> list() {
        log.info("开始处理查询相册列表的业务");
        return mapper.list();
    }

    @Override
    public void delete(Long id) {
        AlbumVO result = mapper.selectById(id);
        if(result==null){
            String message = "数据库中无数据";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.info("开始执行删除");
        mapper.delete(id);
    }

}
